package com.net.j8583.field;


import com.net.j8583.Protocol8583;
import com.net.utils.ByteUtil;
import com.net.utils.StringUtils;

import java.io.UnsupportedEncodingException;

/**
 *
 * Date: 13-11-15
 * Time: 13:39
 */
public abstract class AbstractField implements Field {

    protected String encoding = "GBK";

    protected byte[] reqSrcBytes = new byte[]{};

    protected String reqSrcValue;

    protected byte[] reqEncodeByte = new byte[]{};

    protected String reqEncodeValue;

    protected boolean joinBitMap = true;

    protected Protocol8583 i8583Request;

    protected int fieldLen = 0;

    protected byte[] respSrcBytes = new byte[]{};

    protected String respSrcValue;

    protected byte[] respDecodeBytes = new byte[]{};

    protected String respDecodeValue;

    public int getFieldLen() {
        return fieldLen;
    }

    public void setFieldLen(int fieldLen) {
        this.fieldLen = fieldLen;
    }

    protected AbstractField() {
    }

    public AbstractField(int fieldLen) {
        this(fieldLen, true);
    }

    public AbstractField(int fieldLen, boolean joinBitMap) {
        this.fieldLen = fieldLen;
        this.joinBitMap = joinBitMap;
    }

    protected Integer fieldId;

    public Integer getFieldId() {
        return fieldId;
    }

    public void setFieldId(Integer fieldId) {
        this.fieldId = fieldId;
    }


    public abstract byte[] toByte();

    public String getEncoding() {
        return encoding;
    }


    @Override
    public byte[] getReqSrcBytes() {
        return reqSrcBytes;
    }

    @Override
    public void setReqSrcValue(String reqSrcValue) {
        this.reqSrcValue = reqSrcValue;
        this.reqSrcBytes = str2byte(reqSrcValue);
    }


    public byte[] str2byte(String str) {
        try {
            byte[] byteData = str.getBytes(getEncoding());
            return byteData;
        } catch (UnsupportedEncodingException e) {
            return reqSrcValue.getBytes();
        }
    }

    public String byte2Str(byte[] bytes) {
        try {
            return new String(bytes, getEncoding());
        } catch (UnsupportedEncodingException e) {
            return new String(bytes);
        }
    }

    @Override
    public void setProtocol(Protocol8583 i8583Request) {
        this.i8583Request = i8583Request;
    }

    public String getReqSrcValue() {
        return reqSrcValue;
    }


    public String requestInfo() {
        StringBuilder builder = new StringBuilder();
        builder.append(StringUtils.STR_LEFT_SPLIT1);
        builder.append("request fieldId :" + getFieldId());
        builder.append(",fieldType:" + getFieldType());
        builder.append(",reqSrcValue:" + getReqSrcValue());
        builder.append(",reqEncodeValue:" + reqEncodeValue);
        builder.append(",encoding:" + getEncoding());
        builder.append(",reqSrcBytes:" + ByteUtil.showByteStr(getReqSrcBytes()));
        builder.append(",reqEncodeByte:" + ByteUtil.showByteStr(reqEncodeByte));
        builder.append(StringUtils.STR_RIGHT_SPLIT1);
        return builder.toString();
    }

    public String responseInfo() {
        StringBuilder builder = new StringBuilder();
        builder.append(StringUtils.STR_LEFT_SPLIT1);
        builder.append("response fieldId :" + getFieldId());
        builder.append(",fieldType:" + getFieldType());
        builder.append(",respSrcValue:" + respSrcValue);
        builder.append(",respDecodeValue:" + respDecodeValue);
        builder.append(",encoding:" + getEncoding());
        builder.append(",respSrcBytes:" + ByteUtil.showByteStr(respSrcBytes));
        builder.append(",respDecodeBytes:" + ByteUtil.showByteStr(respDecodeBytes));
        builder.append(StringUtils.STR_RIGHT_SPLIT1);
        return builder.toString();

    }

    @Override
    public boolean isAutoFill() {
        return false;
    }

    public boolean joinBitMap() {
        return joinBitMap;
    }

    @Override
    public void verify() {
    }

    public int getEncodeLen() {
        return fieldLen;
    }

    @Override
    public String getRespDecodeValue() {
        return respDecodeValue;
    }
}
